--
-- Author: ping.wang
-- Date: 2019-05-01 09:57:27
--
-- NewGuildRankRender
--
local NewguildIconRender = require("app.newGuild.views.renders.NewguildIconRender")
local ClassRef = app.mvc.createListViewCell(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.NewGuildRankRender))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)
    self._guildIcon = NewguildIconRender.create(self._ui.Panel:findChild("icon")) -- 公会头像

    self._ui.Button_1:setVisible(true)
    self._ui.Button_1:addClickEventListener(function()
        if self._data and self._data.guildInfo then
            self:dispatchCellEvent("showGuildDetail", self._data.guildInfo)
        end
    end)

    self:setContentSize( self._ui.Panel:getContentSize() )
    self:refresh()
end

--[[
    rank
    guildIcon
    serverName
    guildName
    guildId
    value1
    value2
]]
function ClassRef:refresh()
    if not self._data then
        return
    end
    local data = self._data
    self._ui.Panel_1:setVisible(false)
    self._ui.Panel_2:setVisible(false)
    self._ui.Panel_3:setVisible(false)
    self._ui["Panel/num"]:setVisible(false)
    if data.rank == 1 then
        self._ui.Panel_1:setVisible(true)
    elseif data.rank == 2 then
        self._ui.Panel_2:setVisible(true)
    elseif data.rank == 3 then
        self._ui.Panel_3:setVisible(true)
    else
        self._ui["Panel/num"]:setVisible(true)
        self._ui["Panel/num"]:setString(tostring(data.rank))
    end


    if data.guildIcon then
        self._guildIcon:setVisible(true)
        self._guildIcon:setData(data.guildIcon)
    else
        self._guildIcon:setVisible(false)
    end

    if data.serverName then
        self._ui["Panel/server"]:setVisible(true)
        self._ui["Panel/name"]:setPositionY(59)
        self._ui["Panel/server"]:setString(data.serverName)
    else
        self._ui["Panel/server"]:setVisible(false)
        self._ui["Panel/name"]:setPositionY(47)
    end
    self._ui["Panel/name"]:setString(data.guildName or "")

    if data.isShowDetail then
        self._ui.Button_1:setVisible(true)
    else
        self._ui.Button_1:setVisible(false)
    end

    self._ui["Panel/job"]:setString(data.value1 or "")
    self._ui["Panel/time"]:setString(data.value2 or "")

    
    local isMyGuild = UD:getGuildId() == data.guildId
    self._ui.Panel_bg:findChild("bg_my"):setVisible(isMyGuild)
    self._ui.Panel_bg:findChild("bg"):setVisible(not isMyGuild)
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end


return ClassRef
